# map
def f(num):
    return num * num

r = map(f, list(range(10)))

print(list(r))

# reduce
from functools import reduce
def add(x,y):
    return x + y
print(reduce(add, list(range(5))))

# filter
def is_odd(num):
    if not isinstance(num, int):
        raise TypeError('非整型')
    return num % 2 == 0
l = [x ** 2 for x in range(10)]
print(list(filter(is_odd, l)))

# sorted
sorted([36, -20,7,-90], key=abs, reverse = True)

# 练习
'''
假设我们用一组tuple表示学生名字和成绩：
'''
L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]
def by_name(t):
    return t[0]
L2 = sorted(L, key=by_name)
print(L2)
